package com.lmz.project.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class OrderService {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String KEY_PREFIX = "order:";
    public void addOrder() {
        int keyId = ThreadLocalRandom.current().nextInt(10000) + 1;
        String serialNo = UUID.randomUUID().toString();

        String key = KEY_PREFIX + keyId;
        String value = "京东订单" + serialNo;
        redisTemplate.opsForValue().set(key, value);
    }

    public String getOrder(int keyId) {
        String key = KEY_PREFIX + keyId;
        return (String) redisTemplate.opsForValue().get(key);
    }
}
